package com.example.library.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.library.common.PageResult;
import com.example.library.common.Result;
import com.example.library.dto.BaseQueryRequest;
import com.example.library.entity.Category;
import com.example.library.service.CategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 图书分类控制器
 *
   */
@Slf4j
@RestController
@RequestMapping("/api/categories")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    /**
     * 分页查询分类列表
     */
    @GetMapping
    public PageResult<Category> getCategoryPage(BaseQueryRequest queryRequest) {
        IPage<Category> page = categoryService.getCategoryPage(queryRequest);
        return PageResult.success(page);
    }

    /**
     * 获取分类树
     */
    @GetMapping("/tree")
    public Result<List<Category>> getCategoryTree() {
        List<Category> tree = categoryService.getCategoryTree();
        return Result.success(tree);
    }

    /**
     * 获取所有启用的分类
     */
    @GetMapping("/enabled")
    public Result<List<Category>> getEnabledCategories() {
        List<Category> categories = categoryService.getEnabledCategories();
        return Result.success(categories);
    }

    /**
     * 根据父分类ID获取子分类
     */
    @GetMapping("/children/{parentId}")
    public Result<List<Category>> getChildCategories(@PathVariable Long parentId) {
        List<Category> children = categoryService.getChildCategories(parentId);
        return Result.success(children);
    }

    /**
     * 根据ID获取分类信息
     */
    @GetMapping("/{id}")
    public Result<Category> getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 创建分类
     */
    @PostMapping
    public Result<Void> createCategory(@Valid @RequestBody Category category) {
        boolean success = categoryService.createCategory(category);
        return success ? Result.success("分类创建成功") : Result.error("分类创建失败");
    }

    /**
     * 更新分类
     */
    @PutMapping("/{id}")
    public Result<Void> updateCategory(@PathVariable Long id, @Valid @RequestBody Category category) {
        category.setId(id);
        boolean success = categoryService.updateCategory(category);
        return success ? Result.success("分类更新成功") : Result.error("分类更新失败");
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteCategory(@PathVariable Long id) {
        boolean success = categoryService.deleteCategory(id);
        return success ? Result.success("分类删除成功") : Result.error("分类删除失败");
    }

    /**
     * 批量删除分类
     */
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteCategories(@RequestBody List<Long> categoryIds) {
        boolean success = categoryService.batchDeleteCategories(categoryIds);
        return success ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }

    /**
     * 更新分类状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateCategoryStatus(@PathVariable Long id, @RequestParam Integer status) {
        boolean success = categoryService.updateCategoryStatus(id, status);
        return success ? Result.success("状态更新成功") : Result.error("状态更新失败");
    }

    /**
     * 批量更新分类状态
     */
    @PutMapping("/batch/status")
    public Result<Void> batchUpdateCategoryStatus(@RequestBody List<Long> categoryIds, @RequestParam Integer status) {
        boolean success = categoryService.batchUpdateCategoryStatus(categoryIds, status);
        return success ? Result.success("批量状态更新成功") : Result.error("批量状态更新失败");
    }

    /**
     * 获取分类统计信息
     */
    @GetMapping("/statistics")
    public Result<Object> getCategoryStatistics() {
        Object statistics = categoryService.getCategoryStatistics();
        return Result.success(statistics);
    }

    /**
     * 检查分类名称是否存在
     */
    @GetMapping("/check/name")
    public Result<Boolean> checkCategoryName(@RequestParam String name, @RequestParam(required = false) Long parentId) {
        boolean exists = categoryService.existsByName(name, parentId);
        return Result.success(exists);
    }

}